package com.tanlu.tcc.mq;

import com.rabbitmq.client.Channel;
import com.tanlu.tcc.annotation.TransactionStatus;
import com.tanlu.tcc.domain.Command;
import com.tanlu.tcc.annotation.Propagation;
import com.tanlu.tcc.domain.Participant;
import com.tanlu.tcc.domain.Transaction;
import com.tanlu.tcc.manager.TransactionManager;
import com.tanlu.tcc.util.TransactionXid;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RabbitListener(queues="transaction")
public class Receiver {

    @Autowired
    TransactionManager transactionManager;

    @RabbitHandler
    public void process(Command command, Message message, Channel channel) {
        TransactionStatus transactionStatus = command.getTransactionStatus();
        Propagation propagation = command.getPropagation();
        System.out.println(command.getTransactionStatus());
        if(transactionStatus == TransactionStatus.Try) {
            Participant participant = (Participant)command.getObject();
            if ((propagation.equals(Propagation.REQUIRED) && !transactionManager.isTransactionActive()) ||
                    propagation.equals(Propagation.REQUIRES_NEW)) {
                System.out.println("New Transaction");
                TransactionXid xid = new TransactionXid();
                Date currentDate = new Date();
                Transaction transaction = new Transaction();
                transaction.setTransactionId(xid);
                participant.setTransactionId(xid);
                System.out.println(participant.getServiceName());
                transaction.addParticipant(participant);
                transaction.setOutTime(command.getOuttime());
                transaction.setCreateTime(currentDate);
                transaction.setLastUpdateTime(currentDate);
                transaction.setStatus(TransactionStatus.Try);
                System.out.println(transaction + ":" + transaction.getTransactionId());
                transactionManager.beginTransaction(transaction);
                //调用当前的业务实现Try
                //通过消息中间件发送消息将事务注册到事务管理器中
            } else if ((propagation.equals(Propagation.REQUIRED) || propagation.equals(Propagation.MANDATORY)) && transactionManager.isTransactionActive()) {
                System.out.println("Join Transaction");

                Transaction transaction = transactionManager.getCurrentTransaction();
                System.out.println(transaction + ":" + transaction.getTransactionId());
                //加入以后的当前事务
                participant.setTransactionId(transaction.getTransactionId());
                transaction.addParticipant(participant);
            } else {
                //没有事务
            }
        } else if(transactionStatus == TransactionStatus.Confirm){
            System.out.println("..........service confirm............");
            transactionManager.commit();
        } else if(transactionStatus == TransactionStatus.Cancel){
            transactionManager.rollback();
        } else if(transactionStatus == TransactionStatus.End) {
            Transaction transaction = (Transaction)command.getObject();
            transactionManager.endTranscation(transaction);
        }
        /*try {
            //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e) {
            e.printStackTrace();
        }*/
    }
}




